﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using SlamDunk.CMS.Model.DataModel;

namespace SlamDunk.CMS.Model.ViewModel
{
    /// <summary>
    /// Channel视图模型类（用于添加/修改信息）
    /// </summary>
    public class ChannelView
    {
        #region 01.属性——Id
        /// <summary>
        /// 属性——Id
        /// </summary>
        public int Id { get; set; }
        #endregion

        #region 02.属性——父Id
        /// <summary>
        /// 属性——父Id
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [RegularExpression(@"\d+", ErrorMessage = "必须是数字")]
        [DisplayName("上级栏目")]
        public int PId { get; set; }
        #endregion

        #region 03.属性——栏目名称
        /// <summary>
        /// 属性——栏目名称
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [StringLength(32, ErrorMessage = "名称过长")]
        [DisplayName("栏目名称")]
        public string ChannelName { get; set; }
        #endregion

        #region 04.属性——栏目缩写
        /// <summary>
        /// 属性——Abbr
        /// </summary>
        [DisplayName("栏目缩写")]
        [StringLength(16, ErrorMessage = "内容过长")]
        public string Abbr { get; set; }
        #endregion

        #region 05.属性——栏目描述
        /// <summary>
        /// 属性——栏目描述
        /// </summary>
        [DisplayName("栏目描述")]
        [StringLength(256, ErrorMessage = "内容过长")]
        public string Description { get; set; }
        #endregion

        #region 06.属性——聚合页模板路径
        /// <summary>
        /// 属性——聚合页模板路径
        /// </summary>
        [DisplayName("聚合页模板")]
        [StringLength(256, ErrorMessage = "内容过长")]
        public string IndexTemplate { get; set; }
        #endregion

        #region 07.属性——列表页模板路径
        /// <summary>
        /// 属性——列表页模板路径
        /// </summary>
        [DisplayName("列表页模板")]
        [StringLength(256, ErrorMessage = "内容过长")]
        public string ListTemplate { get; set; }
        #endregion

        #region 08.属性——内容页模板路径
        /// <summary>
        /// 属性——内容页模板路径
        /// </summary>
        [DisplayName("内容页模板")]
        [StringLength(256, ErrorMessage = "内容过长")]
        public string DetailTemplate { get; set; }
        #endregion

        #region 09.属性——页容量
        /// <summary>
        /// 属性——页容量
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [Range(1, 50)]
        [DisplayName("页容量")]
        public int PageSize { get; set; }
        #endregion

        #region 10.属性——排序
        /// <summary>
        /// 属性——排序
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [RegularExpression(@"\d+", ErrorMessage = "必须是数字")]
        [Range(0, 100000, ErrorMessage = "数字过大")]
        [DisplayName("排序")]
        public int? Sort { get; set; }
        #endregion

        #region 11.属性——是否是链接
        /// <summary>
        /// 属性——是否是链接
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [DisplayName("是否是链接")]
        public bool IsLink { get; set; }
        #endregion

        #region 12.属性——是否是导航
        /// <summary>
        /// 属性——是否是导航
        /// </summary>
        [Required(ErrorMessage = "必填")]
        [DisplayName("是否是导航")]
        public bool IsNavigator { get; set; }
        #endregion

        #region 13.属性——链接地址
        /// <summary>
        /// 属性——链接地址
        /// </summary>
        public string Url { get; set; }
        #endregion

        #region 14.方法——转换为数据模型
        /// <summary>
        /// 转换为数据模型
        /// </summary>
        /// <returns>Channel数据模型对象</returns>
        public Channel ToDataModel()
        {
            return new Channel()
            {
                Id = this.Id,
                PId = this.PId,
                ChannelName = this.ChannelName,
                Abbr = this.Abbr,
                Description = this.Description,
                SaveDirectory = string.Format("{0}/{1}", ConfigurationManager.AppSettings["HtmlStorePrefix"], this.Abbr),
                IndexTemplate = this.IndexTemplate,
                ListTemplate = this.ListTemplate,
                DetailTemplate = this.DetailTemplate,
                PageSize = this.PageSize,
                Sort = this.Sort,
                IsLink = this.IsLink,
                IsNavigator = this.IsNavigator,
                DelFlag = false,
                AddTime = DateTime.Now
            };
        }
        #endregion
    }
}
